/**
 * Copyright: Copyright (c) 2016 
 * Company:东方网力科技股份有限公司
 * 
  * @author wangshang
  * @date 2016年8月5日 下午2:16:12
 * @version V1.0
 */
package com.cyy.demo.utils;

import org.apache.commons.lang.StringUtils;

/**
 * @ClassName: UserAgentUtil
 * @Description: 根据 user agent string 来判断出客户端的浏览器以及平台等信息工具类
 * @author wangshang
 * @date 2016年8月5日 下午2:16:12
 *
 */
public final class UserAgentUtil {

    /**
     *  空字符串
     */
    private static final String BLANK_STR = " ";

    private UserAgentUtil(){
        
    }
    
    /**
     * @Title: getUserAgent
     * @Description: 根据客户端 User Agent Strings 判断其浏览器、操作平台 
     * @param userAgent
     *            客户传来的 user-agent
     * @return 返回客户端信息对象
     */
    public static UserAgent getUserAgent(String userAgent) {
        if (StringUtils.isBlank(userAgent)) {
            return null;
        }
        UserAgent userAgenObj = null;
        // 主流应用靠前
        if (userAgent.contains("Windows")) {
            // Windows 10
            if (userAgent.contains("Windows NT 6.4") || userAgent.contains("Windows NT 10.0")) {
                // 判断浏览器
                userAgenObj = judgeBrowser(userAgent, "Windows", "10", null);
            } else if (userAgent.contains("Windows NT 6.2")) {
                // 判断浏览器 Windows 8
                userAgenObj = judgeBrowser(userAgent, "Windows", "8", null);
            } else if (userAgent.contains("Windows NT 6.1")) {
                // Windows 7
                userAgenObj = judgeBrowser(userAgent, "Windows", "7", null);
            }  else if (userAgent.contains("Windows NT 5.2")) {
                // Windows XP x64Edition
                userAgenObj = judgeBrowser(userAgent, "Windows", "XP", "x64 Edition");
            } else if (userAgent.contains("Windows NT 5.1")) {
                // Windows XP
                userAgenObj = judgeBrowser(userAgent, "Windows", "XP", null);
            }
        } else if (userAgent.contains("Mac OS X")) {
            if (userAgent.contains("iPod")) {
                // 判断浏览器
                userAgenObj = judgeBrowser(userAgent, "iPod", null, null);
            }
        }
        return userAgenObj;
    }

    /**
     * @Title: judgeBrowser
     * @Description: 根据客户端 User Agent Strings 判断其浏览器
     * @param userAgent
     *            ： 客户传来的 user-agent
     * @param platformType
     *            :平台
     * @param platformSeries
     *            :系列
     * @param platformVersion
     *            :版本
     * @return UserAgent 信息
     */
    private static UserAgent judgeBrowser(String userAgent, String platformType, String platformSeries, String platformVersion) {

        UserAgent userAgentObj = null;
        if (userAgent.contains("Chrome") && !userAgent.contains("QQBrowser")) {
            // 拿到User
            // AgentString"Chrome/"之后的字符串,结果形如"24.0.1295.0Safari/537.15"或"24.0.1295.0"
            String temp = userAgent.substring(userAgent.indexOf("Chrome/") + 7);
            String chromeVersion = null;
            // temp形如"24.0.1295.0"
            if (!temp.contains(BLANK_STR)) {
                chromeVersion = temp;
            } else {
                // temp形如"24.0.1295.0 Safari/537.15"
                chromeVersion = temp.substring(0, temp.indexOf(BLANK_STR));
            }
            userAgentObj = new UserAgent("Chrome", chromeVersion, platformType, platformSeries, platformVersion);
        } else if (userAgent.contains("Firefox")) {

            // 拿到UserAgentString"Firefox"
            // 之后的字符串,结果形如"16.0.1Gecko/20121011"或"16.0.1"
            String temp = userAgent.substring(userAgent.indexOf("Firefox/") + 8);
            String ffVersion = null;
            // temp形如"16.0.1"
            if (!temp.contains(BLANK_STR)) {
                ffVersion = temp;
            } else {
                // temp形如"16.0.1 Gecko/20121011"
                ffVersion = temp.substring(0, temp.indexOf(BLANK_STR));
            }
            userAgentObj = new UserAgent("Firefox", ffVersion, platformType, platformSeries, platformVersion);
        } else if (userAgent.contains("QQBrowser")) {
            // QQ浏览器
            String temp = userAgent.substring(userAgent.indexOf("QQBrowser/") + 10);
            String qqBrowserVersion;
            // temp形如"24.0.1295.0"
            if (!temp.contains(BLANK_STR)) {
                qqBrowserVersion = temp;
            } else {
                // temp形如"24.0.1295.0 Safari/537.15"
                qqBrowserVersion = temp.substring(0, temp.indexOf(BLANK_STR));
            }
            userAgentObj = new UserAgent("QQ浏览器", qqBrowserVersion, platformType, platformSeries, platformVersion);
        } else if (userAgent.contains("360SE")) {
            // QQ浏览器
            userAgentObj = new UserAgent("360浏览器", "", platformType, platformSeries, platformVersion);
        } else if (userAgent.contains("SE") && userAgent.contains("MetaSr")) {
            // 搜狗浏览器
            int start = userAgent.lastIndexOf("SE") + 3;
            int end = userAgent.lastIndexOf("MetaSr");
            String version = "未知版本";
            if (start < end) {
                version = userAgent.substring(start, end);
            }
            userAgentObj = new UserAgent("搜狗浏览器", version, platformType, platformSeries, platformVersion);
        } else if (userAgent.contains("MSIE")) {
            if (userAgent.contains("MSIE 10.0")) {
                // Internet Explorer 10
                userAgentObj = new UserAgent("Internet Explorer", "10", platformType, platformSeries, platformVersion);
            } else if (userAgent.contains("MSIE 9.0")) {
                // Internet Explorer 9
                userAgentObj = new UserAgent("Internet Explorer", "9", platformType, platformSeries, platformVersion);
            } else if (userAgent.contains("MSIE 8.0")) {
                // Internet Explorer 8
                userAgentObj = new UserAgent("Internet Explorer", "8", platformType, platformSeries, platformVersion);
            } else if (userAgent.contains("MSIE 7.0")) {
                // Internet Explorer 7
                userAgentObj = new UserAgent("Internet Explorer", "7", platformType, platformSeries, platformVersion);
            } else if (userAgent.contains("MSIE 6.0")) {
                // Internet Explorer 6
                userAgentObj = new UserAgent("Internet Explorer", "6", platformType, platformSeries, platformVersion);
            }
        } else if (userAgent.contains("rv:11")) {
            // Internet Explorer 11
            userAgentObj = new UserAgent("Internet Explorer", "11", platformType, platformSeries, platformVersion);
        } else {
            // 暂时支持以上三个主流.其它浏览器,待续...
            userAgentObj = new UserAgent("", "", platformType, platformSeries, platformVersion);
        }
        return userAgentObj;
    }
}
